package com.witdata.controller.code;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.EnvironmentDto;
import com.witdata.framework.service.code.CodeService;
import com.witdata.framework.service.code.EnvironmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/code/environment")
public class EnvironmentController {
    private final EnvironmentService environmentService;
    private final CodeService codeService;

    @GetMapping("/list")
    public Result<List<EnvironmentDto>> getEnvironmentList() {
        List<EnvironmentDto> environmentList = environmentService.getEnvironmentList();
        return Result.ok(Status.SUCCESS, environmentList);
    }

    @PostMapping("/create")
    public Result<String> createEnvironment(@RequestBody EnvironmentDto environmentDto) {
        environmentService.createEnvironment(environmentDto);
        return Result.ok(Status.CREATESUCCESS);
    }

    @GetMapping("/info")
    public Result<EnvironmentDto> getEnvironment(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "codeId", required = false) Long codeId
    ) {
        EnvironmentDto environmentDto = null;
        if (ObjectUtils.isEmpty(codeId)) {
            environmentDto = environmentService.getEnvironmentById(id);
        } else if (ObjectUtils.isEmpty(id)) {
            environmentDto = codeService.getEnvironmentByCodeId(codeId);
        }
        return Result.ok(Status.SUCCESS, environmentDto);
    }
}
